
/**
* @name rookie.wang
* @author 1480538442@qq.com
* @module 定义归属的模块信息
* @see 定义需要参考的函数、变量，并加入相应的超级连接。
* @version 1.0
* @createdTime 2022-12-02 15:35
* @description 工具类
* @modifyTime 
*/
//拖拽的文章:https://juejin.cn/post/7146482620552413214
const tool: any = {};
// 引入前端加密包
import CryptoJS from "crypto-js";
// 配置配置文件
import sysConfig from "@/config";
/**
 * 暂时弃用，用/hooks/web/useCache代替
 * TODO 整理一下这个文件，用ts规范化
 */
tool.data = {
	set(key, data, datetime = 0) {
		//加密
		if (sysConfig.LS_ENCRYPTION == "AES") {
			data = tool.crypto.AES.encrypt(
				JSON.stringify(data),
				sysConfig.LS_ENCRYPTION_key
			);
		}
		let cacheValue = {
			content: data,
			datetime:
				parseInt(datetime) === 0
					? 0
					: new Date().getTime() + parseInt(datetime) * 1000,
		};
		return localStorage.setItem(key, JSON.stringify(cacheValue));
	},
	get(key) {
		try {
			const value = JSON.parse(localStorage.getItem(key));
			if (value) {
				let nowTime = new Date().getTime();
				if (nowTime > value.datetime && value.datetime != 0) {
					localStorage.removeItem(key);
					return null;
				}
				//解密
				if (sysConfig.LS_ENCRYPTION == "AES") {
					value.content = JSON.parse(
						tool.crypto.AES.decrypt(
							value.content,
							sysConfig.LS_ENCRYPTION_key
						)
					);
				}
				return value.content;
			}
			return null;
		} catch (err) {
			return null;
		}
	},
	remove(table) {
		return localStorage.removeItem(table);
	},
	clear() {
		return localStorage.clear();
	},
}

/*sessionStorage*/
tool.session = {
	/**
	 * 设置缓存
	 * @param {缓存key} table 缓存key
	 * @param {缓存对象} settings 缓存对象
	 * @returns
	 */
	set(table, settings) {
		let _set = JSON.stringify(settings);
		return sessionStorage.setItem(table, _set);
	},
	get(table) {
		var data = sessionStorage.getItem(table);
		try {
			data = JSON.parse(data);
		} catch (err) {
			return null;
		}
		return data;
	},
	remove(table) {
		return sessionStorage.removeItem(table);
	},
	clear() {
		return sessionStorage.clear();
	},
};
/**
 * TODO 用vue-use组件代替
 */
/* Fullscreen */
tool.screen = function (element) {
	var isFull = !!(
		document.webkitIsFullScreen ||
		document.mozFullScreen ||
		document.msFullscreenElement ||
		document.fullscreenElement
	);
	if (isFull) {
		if (document.exitFullscreen) {
			document.exitFullscreen();
		} else if (document.msExitFullscreen) {
			document.msExitFullscreen();
		} else if (document.mozCancelFullScreen) {
			document.mozCancelFullScreen();
		} else if (document.webkitExitFullscreen) {
			document.webkitExitFullscreen();
		}
	} else {
		if (element.requestFullscreen) {
			element.requestFullscreen();
		} else if (element.msRequestFullscreen) {
			element.msRequestFullscreen();
		} else if (element.mozRequestFullScreen) {
			element.mozRequestFullScreen();
		} else if (element.webkitRequestFullscreen) {
			element.webkitRequestFullscreen();
		}
	}
};
/**
 * TODO 用vue-use组件代替
 */
/* 复制对象 */
tool.objCopy = function (obj) {
	return JSON.parse(JSON.stringify(obj));
};

/* 日期格式化 */
tool.dateFormat = function (date, fmt = "yyyy-MM-dd hh:mm:ss") {
	date = new Date(date);
	var o = {
		"M+": date.getMonth() + 1, //月份
		"d+": date.getDate(), //日
		"h+": date.getHours(), //小时
		"m+": date.getMinutes(), //分
		"s+": date.getSeconds(), //秒
		"q+": Math.floor((date.getMonth() + 3) / 3), //季度
		S: date.getMilliseconds(), //毫秒
	};
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(
			RegExp.$1,
			(date.getFullYear() + "").substr(4 - RegExp.$1.length)
		);
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(
				RegExp.$1,
				RegExp.$1.length == 1
					? o[k]
					: ("00" + o[k]).substr(("" + o[k]).length)
			);
		}
	}
	return fmt;
};

/* 千分符 */
tool.groupSeparator = function (num) {
	num = num + "";
	if (!num.includes(".")) {
		num += ".";
	}
	return num
		.replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
			return $1 + ",";
		})
		.replace(/\.$/, "");
};

/**
 * 已经抽离出去了
 */
/* 常用加解密 */
tool.crypto = {
	//MD5加密
	MD5(data) {
		return CryptoJS.MD5(data).toString();
	},
	//BASE64加解密
	BASE64: {
		encrypt(data) {
			return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(data));
		},
		decrypt(cipher) {
			return CryptoJS.enc.Base64.parse(cipher).toString(
				CryptoJS.enc.Utf8
			);
		},
	},
	//AES加解密
	AES: {
		encrypt(data, secretKey, config = {}) {
			if (secretKey.length % 8 != 0) {
				console.warn(
					"[error]: 秘钥长度需为8的倍数，否则解密将会失败。"
				);
			}
			const result = CryptoJS.AES.encrypt(
				data,
				CryptoJS.enc.Utf8.parse(secretKey),
				{
					iv: CryptoJS.enc.Utf8.parse(config.iv || ""),
					mode: CryptoJS.mode[config.mode || "ECB"],
					padding: CryptoJS.pad[config.padding || "Pkcs7"],
				}
			);
			return result.toString();
		},
		decrypt(cipher, secretKey, config = {}) {
			const result = CryptoJS.AES.decrypt(
				cipher,
				CryptoJS.enc.Utf8.parse(secretKey),
				{
					iv: CryptoJS.enc.Utf8.parse(config.iv || ""),
					mode: CryptoJS.mode[config.mode || "ECB"],
					padding: CryptoJS.pad[config.padding || "Pkcs7"],
				}
			);
			return CryptoJS.enc.Utf8.stringify(result);
		},
	},
};
/**
 * 暂时未替换
 */
tool.random = function (len) {
	len = len || 32;
	let $chars =
		"ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678"; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
	let maxPos = $chars.length;
	let pwd = "";
	for (let i = 0; i < len; i++) {
		pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
	}
	return pwd;
};

export default tool;